from bokeh.plotting import figure, show
from bokeh.layouts import row
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource
import numpy as np
import pandas as pd

import pandas_bokeh

from bokeh.models.widgets import DataTable, TableColumn
from bokeh.models import ColumnDataSource




np.random.seed(55)
class BokehMgr:
    def plot_line(self, df, y_col, **kwargs):
        df.plot_bokeh(kind="line",y=[y_col,'open'], **kwargs)

    def show(self,df):
        data_table = DataTable(
            columns=[TableColumn(field=Ci, title=Ci) for Ci in df.columns],
            source=ColumnDataSource(df),
            height=300,
        )

        # 创建散点图:
        p_line = df.plot_bokeh.line(
            #x="petal length(cm)",
            y="equity",
            #category="species",
            title="000300_equity曲线",
            show_figure=False,
            rangetool=True,
        )

        p_line2 = df.plot_bokeh.line(
            # x="petal length(cm)",
            y="close",
            # category="species",
            title="000300_equity曲线",
            show_figure=False,
            rangetool=True,
        )

        data = {
            'fruits':
                ['苹果', '梨', '草莓', '西瓜', '葡萄', '香蕉'],
            '2015': [2, 1, 4, 3, 2, 4],
            '2016': [5, 3, 3, 2, 4, 6],
            '2017': [3, 2, 4, 4, 5, 3]
        }
        df = pd.DataFrame(data).set_index("fruits")

        p_bar = df.plot_bokeh.bar(
            ylabel="每斤的的价格 [￥]",
            title="水果每年的价格",
            show_figure=False,
            alpha=0.6)

        # Combine Table and Scatterplot via grid layout:
        pandas_bokeh.plot_grid([[p_line,p_bar],[p_line2,data_table]], plot_width=400, plot_height=350)



if __name__ == '__main__':
    from common.config import D
    df = D.load(['000300.SH'])
    df.index = pd.to_datetime(df.index)
    df['equity'] = (1+df['rate']).cumprod()
    print(df)
    del df['date']
    BokehMgr().show(df)